﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;
using System.Configuration;
using System.Web;

namespace MBSBusinessEntities
{
    public class BookingBE : ICrud
    {

        #region ClassVariables
        private MBS_Entities mbs = new MBS_Entities();
        #endregion ClassVariables

        #region Constructor
        public BookingBE()
        {
            //empty constructor
        }
        public BookingBE(DateTime BookingDate, string Seatnumbers, int ShowTimeID, int UserID)
        {
            //this.BookingID = BookingId;
            this.BookingDate = BookingDate;
            this.Seatnumbers = Seatnumbers;
            //this.Amount = Amount;
            //this.RewardPointsGained = RewardPointsGained;
            this.ShowTimeID = ShowTimeID;
            this.UserID = UserID;
            this.mbs = new MBS_Entities();
        }

        #endregion Constructor

        #region Properties
        public int BookingID { get; set; }
        public DateTime? BookingDate { get; set; }
        public string Seatnumbers { get; set; }
        public decimal? Amount { get; set; }
        public int? RewardPointsGained { get; set; }
        public int ShowTimeID { get; set; }
        public int? UserID { get; set; }

        #endregion


        #region Methods

        /*
        public bool AddBooking(int BookingId,DateTime BookingDate, string Seatnumbers, decimal Amount, int RewardPointsGained, int ShowTimeID, int UserID)
        {
            Booking booking = new Booking();
            booking.BookingID = BookingId;
            booking.BookingDate = BookingDate;
            booking.Seatnumbers = Seatnumbers;
            booking.Amount = Amount;
            booking.RewardPointsGained = RewardPointsGained;
            booking.ShowTimeID = ShowTimeID;
            booking.UserID = UserID;
            mbs.AddToBookings(booking);
            int num =  mbs.SaveChanges();
            return num > 0;
        }
        */
        public bool CancelBooking(int BookingID)
        {
            Booking booking = mbs.Bookings.SingleOrDefault(m => m.BookingID == BookingID);
            // Need to add isCancelled column in database..
            //  booking.isCancelled = true;
            int num = mbs.SaveChanges();
            return num > 0;
        }

        public List<BookingBE> getUserBookings(int UserId)
        {
            List<Booking> bookings = mbs.Bookings.Where(m => m.UserID == UserId).ToList();
            List<BookingBE> bookingsBEs = new List<BookingBE>();
            foreach (Booking booking in bookings)
            {
                BookingBE be = ConvertBookingToBookingBE(booking);

                bookingsBEs.Add(be);
            }

            return bookingsBEs;
        }

        public BookingBE ConvertBookingToBookingBE(Booking booking)
        {
            if (booking == null) return null;
            BookingBE bookingBE = new BookingBE();
            bookingBE.BookingDate = booking.BookingDate;
            bookingBE.Seatnumbers = booking.Seatnumbers;
            bookingBE.Amount = booking.Amount;
            bookingBE.RewardPointsGained = booking.RewardPointsGained;
            bookingBE.ShowTimeID = booking.ShowTimeID;
            bookingBE.UserID = booking.UserID;

            return bookingBE;
        }

        public Booking bookingById(int id)
        {
            Booking booking = mbs.Bookings.SingleOrDefault(m => m.BookingID == id);
            return booking;
        }

        //If i give a showtimeid i should get all the bookings associated to it
        //Particular user and showtimeid is given i should get his booking object(The booked seats list i think)


        #endregion

        #region ICrud
        public int AddBooking()
        {
            Booking booking = new Booking();
            int id;
            booking.BookingDate = this.BookingDate;
            booking.Seatnumbers = this.Seatnumbers;
            booking.Amount = this.Amount;
            booking.RewardPointsGained = this.RewardPointsGained;
            booking.ShowTimeID = this.ShowTimeID;
            if (this.UserID > 0)
            {
                booking.UserID = this.UserID;
            }
            mbs.AddToBookings(booking);
            int num = mbs.SaveChanges();
            if (num <= 0)
            {
                id = 0;
            }
            else
            {
                id = booking.BookingID;
            }
            return id;
           
        }
        public bool Add()
        {
            Booking booking = new Booking();
            booking.BookingDate = this.BookingDate;
            booking.Seatnumbers = this.Seatnumbers;
            booking.Amount = this.Amount;
            booking.RewardPointsGained = this.RewardPointsGained;
            booking.ShowTimeID = this.ShowTimeID;
            if (booking.UserID > 0)
            {
                booking.UserID = this.UserID;
            }
            mbs.AddToBookings(booking);
            int num = mbs.SaveChanges();
            return num > 0;
            //return false;
        }
        public bool Delete(int id)
        {
            Booking booking = bookingById(id);
            booking.IsDeleted = true;
            int num = mbs.SaveChanges();
            return num > 0;
        }

        public bool Update()
        {
            Booking booking = bookingById(this.BookingID);
            booking.BookingDate = this.BookingDate;
            booking.Seatnumbers = this.Seatnumbers;
            booking.Amount = this.Amount;
            booking.RewardPointsGained = this.RewardPointsGained;
            booking.ShowTimeID = this.ShowTimeID;
            if (UserID != 0 && UserID != null)
            {
                booking.UserID = UserID;
            }
            int num = mbs.SaveChanges();
            return num > 0;
        }

        public object GetByID(int id)
        {
            Booking booking = bookingById(id);
            BookingBE bookingBE = this.ConvertBookingToBookingBE(booking);

            if (Validate_Retrieval(bookingBE))
            {
                return bookingBE;
            }

            return null;
        }

        private bool Validate_Retrieval(BookingBE bookingBE)
        {

            if (bookingBE == null)
            {
                return false;
            }

            return true;
        }

        #endregion ICrud

        #region

        private bool isValid=true;

        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }


        public bool Validate_Inserition()
        {

            
            if (this.BookingID==0){isValid =false;return false;}
            if (this.ShowTimeID!=0){isValid =false;return false;}
                    
            return true;
        }

        //add user points to user database
        // Need to remove this method..I see no use..we already have this method thorugh ICrud..
        /*
        public bool Add(double showTimeFare)
        {
            try
            {
                Booking booking = new Booking();
                // booking.BookingID = BookingId;
                booking.BookingDate = this.BookingDate;
                //In Database this colum is of XML type..so have to make changes
                //    booking.Seatnumbers = Seatnumbers;
                booking.Amount = Convert.ToDecimal(getAmount(showTimeFare));
                booking.RewardPointsGained = getRewardPointsGained(Seatnumbers);
                booking.ShowTimeID = this.ShowTimeID;
                if (UserID != 0 && UserID != null)
                {
                    booking.UserID = UserID;
                }

                mbs.AddToBookings(booking);
                int num = mbs.SaveChanges();
                return num > 0;
            }

            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return false;
            }
            // return true;
        }
        */
        public static string ErrorMessage;

        public double getAmount(double fare)
        {
            int numOfSeats = 0;
            return fare * numOfSeats;
        }
        public int getRewardPointsGained(string seatnumbers)
        {
            String[] seats = seatnumbers.Split(',');
            int seatCount = seats.Length;
            int rewardpoints = Convert.ToInt32(ConfigurationManager.AppSettings["RewardPoints"]);
            return (rewardpoints * seatCount);
        }
        #endregion
        //Get booking ID for ticket
        public int getBookingID(int showtimeid,int userid)
        {
            Booking booking ;

            try
            {

                if (UserID != 0 && UserID != null)
                {
                    booking = (Booking)mbs.Bookings.FirstOrDefault(m => m.UserID == userid && m.ShowTimeID == showtimeid);
                }
                else
                {
                    booking = (Booking)mbs.Bookings.FirstOrDefault(m => m.ShowTimeID == showtimeid);
                }

                return booking.BookingID;
            }

            catch (Exception ex)
            {
                return 0;
            }

            
        }
    }
}